<?php
class LocationController extends Zend_Controller_Action {
	// Mảng tham số nhận được ở mỗi Action:
	protected $_arrParam;
	private $USER_ID = 0;
	private $USER_NAME = "yourtour";
	private $URI = "/";
	private $URL_ACC_LOGIN = "/account/login/";
	private $DEFAULT_PROFILE_PICTURE = "default/img/no_image.jpg";
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	public function init() {
		$this->_arrParam = $this->_request->getParams ();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;
		
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN . '?return_url=' . $this->URI );
		} else {
			// get user info
			$this->USER_ID = $mAccount->getUserId ();
			$this->USER_NAME = $mAccount->getUserName ();
			
			$this->view->fullname = $mAccount->getFullName ();
			$this->view->return_url = $this->URI;
			$this->view->userid = $mAccount->getUserId ();
			$strAvatar = $mAccount->getProfilePicture ();
			if ($strAvatar != "") {
				$this->view->my_avatar = $mAccount->getProfilePicture ();
			} else {
				$this->view->my_avatar = $this->DEFAULT_PROFILE_PICTURE;
			}
			// get report
			$mRep = new Default_Model_Report();
			$arrRep = $mRep->listAllReport();
			$this->view->arrRep = $arrRep;
		}
		// End xu li cho phan header
	}
	public function indexAction() {
		// $strM = ("Hà Nội");
		// echo $strM;
		// echo utf8_decode($strM);
		$mUser = new Default_Model_User ();
		$mCity = new Default_Model_City ();
		$mLoc = new Default_Model_Location ();
		$mPhoto = new Default_Model_Photo ();
		$mLike = new Default_Model_Like();
		
		if ($this->_request->isGet ()) {
			if (isset ( $_GET ['delete'] ) && $_GET ['delete'] == 1) {
				if (isset ( $_GET ['loc_id'] )) {
					// delete location
					$mProcess = new Default_Model_YourTourProcess ();
					$strMessReturn = $mProcess->deleteLocationInfo ( $_GET ['loc_id'] );
					if ($strMessReturn == "") {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Xoá địa điểm thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $strMessReturn;
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xoá.';
				}
			}
		}
		// process to display view
		$this->view->total_location = $mLoc->countLocation ();
		$this->view->my_user_id = $this->USER_ID;
		$arrLoc = null;
		if (isset ( $_GET ['city_id'] ) && $_GET ['city_id'] != "") {
			$arrLoc = $mLoc->getLocationByCity ( $_GET ['city_id'] );
		} else {
			$arrLoc = $mLoc->listAllLocation ();
		}
		
		if (count ( $arrLoc ) != 0) {
			// map location
			$arrMapLocName = null;
			$arrMapLocLat = null;
			$arrMapLocLon = null;
			
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrLoc );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 100 );
			$paginator->setPageRange ( 3 );
			$currentPage = $this->_request->getParam ( 'page', 1 );
			$paginator->setCurrentPageNumber ( $currentPage );
			$this->view->arrLoc = $paginator;
			// get city and user
			$arrUser = null;
			$arrCity = null;
			$arrPhoto = null;
			$arrLike = null;
			foreach ( $paginator as $loc ) {
				$arrMapLocName [] = $loc ['name'];
				$arrMapLocLat [] = $loc ['latitude'];
				$arrMapLocLon [] = $loc ['longitude'];
				$arrUser [] = $mUser->getUserInfoById ( $loc ['user_id'] );
				$arrCity [] = $mCity->getCityInfoById ( $loc ['city_id'] );
				$arrPhoto [] = $mPhoto->getPhotoByLocId ( $loc ['location_id'] );
				$arrLike [] = $mLike->getLikeByLocId($loc ['location_id']);
			}
			$this->view->arrMapLocName = $arrMapLocName;
			$this->view->arrMapLocLat = $arrMapLocLat;
			$this->view->arrMapLocLon = $arrMapLocLon;
			$this->view->arrUser = $arrUser;
			$this->view->arrCity = $arrCity;
			$this->view->arrPhoto = $arrPhoto;
			$this->view->arrLike = $arrLike;
		}
		
		// get all city
		$arrListCity = $mCity->listAllCity ();
		$this->view->arrListCity = $arrListCity;
	}
	public function addAction() {
		$mUser = new Default_Model_User ();
		$mCity = new Default_Model_City ();
		$mLoc = new Default_Model_Location ();
		$mPhoto = new Default_Model_Photo ();
		
		if ($this->_request->isPost ()) {
			if (isset ( $_POST ['btnAdd'] )) {
				$bUploadPhoto = true;
				$strMessErr = "";
				$arrParam = array (
						'city_id' => $_POST ['cbCity'],
						'user_id' => $this->USER_ID,
						'name' =>  ( $_POST ['location_name'] ),
						'description' =>  ( $_POST ['description'] ),
						'latitude' => $_POST ['latval'],
						'longitude' => $_POST ['longval'],
						'created_time' => date ( 'Y-m-d H:i:s' )
				);
				if ($mLoc->insertLocation ( $arrParam )) {
					if (! empty ( $_FILES ['picture'] ['name'] )) {
						try {
							$mImage = new Default_Model_Image ();
							if ($mImage->uploadImg ( $this->USER_NAME )) {
								// get last location id
								$arrLastLoc = $mLoc->getLastLocation ();
								if(count($arrLastLoc) != 0) {
									$iLastLocId = $arrLastLoc [0] ['location_id'];
								}
								// insert to photo table
								$arrParam = array (
										'location_id' => $iLastLocId,
										'photo_link' => $mImage->getImgName (),
										'user_id' => $this->USER_ID,
										'created_time' => date ( 'Y-m-d H:i:s' )
								);
								if (! $mPhoto->insertPhoto ( $arrParam )) {
									$bUploadPhoto = false;
									$strMessErr = $mPhoto->getError ();
								}
							} else {
								$bUploadPhoto = false;
								$strMessErr = $mImage->getError ();
							}
						} catch ( Exception $e ) {
							$bUploadPhoto = false;
							$strMessErr = $e->getMessage ();
						}
					}
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
					$this->view->alert_message = 'Thêm mới địa điểm thành công!';
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> ' . $mLoc->getError ();
				}
			}
		}
		
		$this->view->total_location = $mLoc->countLocation ();
		// get all city
		$arrListCity = $mCity->listAllCity ();
		$this->view->arrListCity = $arrListCity;
	}
	public function editAction() {
		$mUser = new Default_Model_User ();
		$mCity = new Default_Model_City ();
		$mLoc = new Default_Model_Location ();
		$mPhoto = new Default_Model_Photo ();
		
		if ($this->_request->isPost ()) {
			if (isset ( $_POST ['btnEdit'] )) {
				$bUploadPhoto = true;
				$strMessErr = "";
				if (! empty ( $_FILES ['picture'] ['name'] )) {
					try {
						$mImage = new Default_Model_Image ();
						if ($mImage->uploadImg ( $this->USER_NAME )) {
							// delete photo from disk
							$arrPhotoInfo = $mPhoto->getPhotoByLocId ( $_POST ['location_id'] );
							if($mImage->deleteImg($arrPhotoInfo[0]['photo_link'])) {
								// update photo table
								$arrParam = array (
										'photo_link' => $mImage->getImgName ()
								);
								if (! $mPhoto->updatePhotoInfo( $arrParam, $_POST['photo_id'] )) {
									$bUploadPhoto = false;
									$strMessErr = $mPhoto->getError ();
								}
							} else {
								$bUploadPhoto = false;
								$strMessErr = $mImage->getError ();
							}
							
						} else {
							$bUploadPhoto = false;
							$strMessErr = $mImage->getError ();
						}
					} catch ( Exception $e ) {
						$bUploadPhoto = false;
						$strMessErr = $e->getMessage ();
					}
				}
				if ($bUploadPhoto) {
					$arrParam = array (
							'city_id' => $_POST ['cbCity'],
							'name' =>  ( $_POST ['location_name'] ),
							'description' =>  ( $_POST ['description'] ),
							'latitude' => $_POST ['latval'],
							'longitude' => $_POST ['longval'],
							'updated_time' => date ( 'Y-m-d H:i:s' ) 
					);
					if ($mLoc->updateLocationInfo ( $arrParam, $_POST ['location_id'] )) {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Chỉnh sửa thông tin địa điểm thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $mLoc->getError ();
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> ' . $strMessErr;
				}
			}
		}
		
		if (isset ( $_GET ['location_id'] )) {
			// get location info
			$arrLocInfo = $mLoc->getLocationInfoById ( $_GET ['location_id'] );
			if (count ( $arrLocInfo ) == 0) {
				$this->view->display_alert = 1;
				$this->view->alert_type = "";
				$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin địa điểm.';
			} else {
				// get Photo
				
				$arrPhoto = $mPhoto->getPhotoByLocId ( $_GET ['location_id'] );
				if (count ( $arrPhoto ) != 0) {
					$this->view->arrPhoto = $arrPhoto;
				}
				$this->view->arrLocInfo = $arrLocInfo;
			}
		}
		
		$this->view->total_location = $mLoc->countLocation ();
		// get all city
		$arrListCity = $mCity->listAllCity ();
		$this->view->arrListCity = $arrListCity;
	}
	
	public function mapAction() {
		$mLoc = new Default_Model_Location();
		if ($this->_request->isGet ()) {
			if(isset($_GET['loc_id'])) {
				$arrLocInfo = $mLoc->getLocationInfoById($_GET['loc_id']);
				if(count($arrLocInfo) != 0) {
					$this->view->arrLoc = $arrLocInfo;
				} else {
					echo 'Bạn không thể xem bản đồ này';
				}
			} else {
				echo 'Bạn không thể xem bản đồ này';
			}
		}
	}
}